$(function () {
    $('#register').click(function () {
        $(this).attr('disable', true);
        var login = $('#loginId').val();
        var loginPassword = $('#loginPassword').val();
        var loginPasswordRepeat = $('#loginPasswordRepeat').val();
        var verifyCode = $('#verifyCode').val();
        if(!validParams(login, loginPassword, loginPasswordRepeat, verifyCode)){
            $('#register').attr('disable', false);
            return;
        }
        $.ajax({
            type: 'POST',
            url: '/register',
            data: $('#registerForm').serialize(),
            success: function (result) {
                if(result.statusCode === 200){
                    // 显示注册成功，然后跳转到login
                    alert('注册成功！');
                    window.location.href = '/login';
                }else{
                    $('#errorMsg').text(result.message).css('color','#ff0000');
                    // 刷新验证码
                    $('#captcha').trigger('click');
                }
            },
            complete: function (){
                $('#register').attr('disabled', false);
            }
        });
    });

    // 判断三个输入，还有密码一致
    function validParams(login, loginPassword, loginPasswordRepeat, verifyCode) {
        if (!validLoginId(login)) {
            $('#errorMsg').text("手机号格式错误！").css('color', '#ff0000');
            return false;
        } else if (!validPassword(loginPassword)) {
            $('#errorMsg').text("密码长度为6-18位大小写字母、数字！").css('color', '#ff0000');
            return false;
        } else if (loginPassword !== loginPasswordRepeat) {
            $('#errorMsg').text("两次输入的密码不一致").css('color', '#ff0000');
            return false;
        } else if (isEmpty(verifyCode)) {
            $('#errorMsg').text("验证码格式错误！").css('color', '#ff0000');
            return false;
        }
        return true;
    }
});